<template>
  <div class="dashboard-container">
    <div class="top">
      <div class="left">
        <div class="left-top">
          <div class="left-top-left">
            <div class="title">
              <h3>运营人员（当天）</h3>
            </div>
            <div class="count">
              <div class="gd">
                <p>{{ statsList[0].total }}</p>
                <span>工单总数（个）</span>
              </div>
              <div class="gd">
                <p>{{ statsList[0].completedTotal }}</p>
                <span>完成工单（个）</span>
              </div>
              <div class="gd">
                <p>{{ statsList[0].cancelTotal }}</p>
                <span>拒绝工单（个）</span>
              </div>
              <div class="gd">
                <p>{{ statsList[0].workerCount }}</p>
                <span>运营人员数（个）</span>
              </div>
            </div>
          </div>
          <div class="left-top-right">
            <div class="title">
              <h3>运维人员（当天）</h3>
            </div>
            <div class="count">
              <div class="gd">
                <p>{{ statsList[1].total }}</p>
                <span>工单总数（个）</span>
              </div>
              <div class="gd">
                <p>{{ statsList[1].completedTotal }}</p>
                <span>完成工单（个）</span>
              </div>
              <div class="gd">
                <p>{{ statsList[1].cancelTotal }}</p>
                <span>拒绝工单（个）</span>
              </div>
              <div class="gd">
                <p>{{ statsList[1].workerCount }}</p>
                <span>运维人员数（个）</span>
              </div>
            </div>
          </div>
        </div>

        <div class="left-bottom">
          <div class="head">
            <h3>工单状态</h3>
            <div class="time">
              <el-date-picker
                v-model="value1"
                size="mini"
                prefix-icon="el-icon-date"
                style="margin: 3px 10px 0 0; width: 240px"
                type="daterange"
                range-separator="~"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd"
                @change="checked"
              />
              <div class="btn">
                <span
                  :class="statues === 1 ? 'setColor' : ''"
                  @click="changeFn(1)"
                >周</span>
                <span
                  :class="statues === 2 ? 'setColor' : ''"
                  @click="changeFn(2)"
                >月</span>
                <span
                  :class="statues === 3 ? 'setColor' : ''"
                  @click="changeFn(3)"
                >年</span>
              </div>
            </div>
          </div>
          <div class="body">
            <img
              v-if="!arrList.length"
              src="https://likede2-admin.itheima.net/img/empty.87c4f71b.png"
              alt=""
            >
            <div v-show="arrList" ref="line" class="body" />
          </div>
        </div>
        <div class="right-bottom">
          <div class="header">
            <div class="title">人效排名(月度)</div>
            <el-select
              v-model="value"
              size="mini"
              style="width: 80px"
              placeholder=""
            >
              <el-option label="全部" value="全部">全部</el-option>
              <el-option
                v-for="item in options"
                :key="item.id"
                :label="item.name"
                :value="item.name"
              />
            </el-select>
          </div>
          <div class="role-group">
            <span
              :class="people === 1 ? 'setColor1' : ''"
              @click="changePeople(1)"
            >运营人员</span>
            <span
              :class="people === 2 ? 'setColor1' : ''"
              @click="changePeople(2)"
            >运维人员</span>
          </div>
          <div class="empty">
            <img
              src="https://likede2-admin.itheima.net/img/empty.87c4f71b.png"
              alt=""
            >
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getRegion, getStatsList, getWorkState } from '@/api/peoplemanger'
import { mapGetters } from 'vuex'
export default {
  name: 'Dashboard',
  data () {
    return {
      people: 1,
      value: '全部',
      statues: 1,
      echarts1: '',
      value1: [
        `${this.changeTimeFormat(this.$moment().format('dddd'))}`,
        `${this.$moment().format('YYYY-MM-DD')}`
      ],
      statsList: [{ workerCount: 0, cancelTotal: 0, completedTotal: 0, total: 0 },
        { workerCount: 0, cancelTotal: 0, completedTotal: 0, total: 0 }
      ],
      currentTime: this.$moment().format('YYYY-MM-DD'),
      arrList: {},
      options: []
    }
  },
  computed: {
    ...mapGetters(['name'])
  },
  created () {
    this.getWorkList()
    this.getWorkState1()
    this.getRegionList()
  },
  mounted () {},
  methods: {
    checked () {
      this.$nextTick(() => {
        this.getWorkState1()
      })
    },
    changeFn (id) {
      this.statues = id
      if (id === 1) {
        this.$nextTick(() => {
          // 最近一周
          this.value1 = [
            `${this.start = this.changeTimeFormat(this.$moment().format('dddd'))}`,
            `${this.$moment().format('YYYY-MM-DD')}`
          ]
          this.getWorkState1()
        })
      }
      if (id === 2) {
        this.$nextTick(() => {
          this.value1 = [
            `${this.$moment().format('YYYY-MM-01')}`,
            `${this.$moment().format('YYYY-MM-DD')}`
          ]
          this.getWorkState1()
        })
      } else if (id === 3) {
        this.$nextTick(() => {
          this.value1 = [
            `${this.$moment().format('YYYY-01-01')}`,
            `${this.$moment().format('YYYY-MM-DD')}`
          ]
          this.getWorkState1()
        })
      }
    },
    // 处理时间
    changeTimeFormat (time) {
      switch (time) {
        case 'Monday':
          return this.$moment().format('YYYY-MM-DD')
        case 'Tuesday':
          return this.$moment().subtract('1', 'd').format('YYYY-MM-DD')
        case 'Wednesday':
          return this.$moment().subtract('2', 'd').format('YYYY-MM-DD')
        case 'Thursday':
          return this.$moment().subtract('3', 'd').format('YYYY-MM-DD')
        case 'Friday':
          return this.$moment().subtract('4', 'd').format('YYYY-MM-DD')
        case 'Saturday':
          return this.$moment().subtract('5', 'd').format('YYYY-MM-DD')
        case 'Sunday':
          return this.$moment().subtract('6', 'd').format('YYYY-MM-DD')
        default:
          return this.$moment().format('YYYY-MM-DD')
      }
    },
    // 选择人员
    changePeople (id) {
      this.people = id
    },
    // 获取区域列表
    async getRegionList () {
      const { currentPageRecords } = await getRegion()
      // console.log(currentPageRecords)
      this.options = currentPageRecords
    },
    // 工单列表
    async getWorkList () {
      const data = await getStatsList(
        `${this.$moment().format('YYYY-MM-DD 00:00:00')}`,
        `${this.$moment().format('YYYY-MM-DD 23:59:59')}`
      )
      console.log(data)
      this.statsList = data
    },
    // 工单统计
    async getWorkState1 (date = this.value1[0]) {
      const res = await getWorkState(date, this.value1[1])
      const arr = res.map(item => item.collectDate)
      this.arrList = res
      this.$nextTick(() => {
        this.setline(arr, res)
      })
    },

    // 图表
    setline (date) {
      const line = this.$echarts.init(this.$refs.line)
      window.addEventListener('resize', () => {
        line.resize()
      })
      line.setOption({
        xAxis: {
          type: 'category',
          data: date
        },
        yAxis: {
          type: 'value'
        },
        legend: { bottom: 0 },
        series: [
          {
            name: '完成工单',
            type: 'bar',
            data: [1]
          },
          {
            name: '取消工单',
            type: 'bar',
            data: [2]
          }
        ]
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard-container {
  padding: 20px;
  .top {
    width: 100%;
    height: 570px;
    display: flex;
    .left {
      width: 100%;
      height: 80%;
      .left-top {
        display: flex;
        height: 50%;
        .left-top-left {
          width: 50%;
          height: 100%;
          background-color: #e9f3ff;
          background-image: url('../../../assets/下载 (2).png'),
            url('../../../assets/下载.png');
          background-repeat: no-repeat, no-repeat;
          background-position: 0 0, calc(100% - 12px) 100%;
          border-radius: 20px;
          padding: 15px;
          .title {
            margin-top: 15px;
            margin-bottom: 20px;
            font-size: 14px;
            display: flex;
            h3 {
              line-height: 0;
              margin: 0;
            }
            span {
              margin: 0;
              line-height: 0;
              font-size: 12px;
              color: #999;
              margin-left: 10px;
            }
          }
          .count {
            display: flex;
            margin-top: 8%;
            .gd {
              flex: 1;
              display: flex;
              flex-direction: column;
              text-align: center;
              color: #91a7dc;
              font-size: 12px;
            }
            p {
              color: #072074;
              font-size: 36px;
              margin: 0;
              margin: 10px 0;
              font-weight: 700;
            }
          }
        }
        .left-top-right {
          margin-left: 20px;
          width: 50%;
          height: 100%;
          // background-color: #fbefe8;
          background: #fbefe8
            url(https://likede2-admin.itheima.net/img/operation.4120cc58.png)
            no-repeat calc(100% - 12px) 100%;
          border-radius: 20px;
          padding: 15px;
          .title {
            margin-top: 15px;
            margin-bottom: 20px;
            font-size: 14px;
            display: flex;
            h3 {
              line-height: 0;
              margin: 0;
            }
            span {
              margin: 0;
              line-height: 0;
              font-size: 12px;
              color: #999;
              margin-left: 10px;
            }
          }
          .count {
            display: flex;
            margin-top: 8%;
            .gd {
              flex: 1;
              display: flex;
              flex-direction: column;
              text-align: center;
              color: #de9690;
              font-size: 12px;
            }
            p {
              color: #ff5757;
              font-size: 36px;
              margin: 0;
              margin: 10px 0;
              font-weight: 700;
            }
          }
        }
      }
      .left-bottom {
        padding: 20px;
        width: 75%;
        height: 650px;
        background-color: #fff;
        border-radius: 20px;
        margin-top: 20px;
        position: absolute;
        .head {
          display: flex;
          justify-content: space-between;
          margin-bottom: 30px;
          .time {
            display: flex;
            justify-content: flex-end;
          }
          h3 {
            line-height: 0;
            margin: 15px 0 0 10px;
            font-size: 16px;
            color: #333;
          }
          span {
            margin: 0;
            line-height: 0;
            font-size: 12px;
            color: #999;
            margin-left: 10px;
          }
          .btn {
            width: 130px;
            height: 35px;
            background-color: #f7fbff;
            border-radius: 10px;
            display: flex;
            align-items: center;
            justify-content: center;
            span {
              display: block;
              width: 35px;
              height: 25px;
              text-align: center;
              line-height: 25px;
              cursor: pointer;
            }
            .setColor {
              background-color: #fff;
              box-shadow: 0 0 3px;
              border-radius: 5px;
            }
          }
        }
        .body {
          // background-color: pink;
          text-align: center;
          height: 550px;
          line-height: 550px;
        }
      }
      .right-bottom {
        float: right;
        width: 22%;
        margin: 20px 3px;
        height: 142%;
        border-radius: 20px;
        background-color: #fff;
        .header {
          padding: 20px;
          display: flex;
          justify-content: space-between;
          height: 32px;
          width: 100%;
          // margin-top: 20px;
          color: #333;
          .title {
            font-size: 14px;
            line-height: 32px;
            font-weight: 600;
          }
        }
        .role-group {
          margin: 0 auto;
          margin-top: 20px;
          text-align: center;
          font-size: 14px;
          color: #333;
          font-weight: 600;
          width: 170px;
          height: 34px;
          line-height: 34px;
          background: rgba(233, 243, 255, 0.37);
          border-radius: 10px;
          span {
            margin-right: 10px;
          }
          .setColor1 {
            background-color: #fff;
            box-shadow: 0 0 4px 0 rgb(0 0 0 / 11%);
            padding: 2px 5px;
            border-radius: 7px;
          }
        }
        .empty {
          width: 100%;
          height: 100%;
          line-height: 560px;
          text-align: center;
        }
      }
    }
  }
}
</style>
